package com.wuliu.business.sys;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.wuliu.model.sys.Role;
import com.wuliu.util.DbPool;

public class RoleObj {
	public static Role getRoleById(String id){
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		Role role = new Role();
		if(id==null || id.equals(""))
			return role;
		String strsql = "SELECT * FROM WL_ROLE WHERE ROLE_ID=?";
		try{
			conn = DbPool.getConnection();
			pstmt = conn.prepareStatement(strsql);
			pstmt.setString(1,id);
			rs = pstmt.executeQuery();
			if(rs.next()){
				role.setRoleId(rs.getString("ROLE_ID"));
				role.setRoleName(rs.getString("ROLE_NAME"));
				role.setRoleDesc(rs.getString("ROLE_DESC"));
				role.setAddBy(rs.getString("ADD_BY"));
				role.setAddDate(rs.getString("ADD_DATE"));
				role.setAddTime(rs.getString("ADD_TIME"));
				role.setUpdBy(rs.getString("UPD_BY"));
				role.setUpdDate(rs.getString("UPD_DATE"));
				role.setUpdTime(rs.getString("UPD_TIME"));
				role.setActiveFlag(rs.getString("ACTIVE_FLAG"));
			}
		}catch(SQLException sqlE){
			sqlE.printStackTrace();
		}finally{
			DbPool.close(conn, pstmt, rs);
		}
		return role;
	}
	
	public static List<Role> getRoleList(){
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		
		List<Role> list = new ArrayList<Role>();
		String strsql = "SELECT * FROM WL_ROLE ";
		try{
			conn = DbPool.getConnection();
			pstmt = conn.prepareStatement(strsql);
			rs = pstmt.executeQuery();
			while(rs.next()){
				Role role = new Role();
				role.setRoleId(rs.getString("ROLE_ID"));
				role.setRoleName(rs.getString("ROLE_NAME"));
				role.setRoleDesc(rs.getString("ROLE_DESC"));
				role.setAddBy(rs.getString("ADD_BY"));
				role.setAddDate(rs.getString("ADD_DATE"));
				role.setAddTime(rs.getString("ADD_TIME"));
				role.setUpdBy(rs.getString("UPD_BY"));
				role.setUpdDate(rs.getString("UPD_DATE"));
				role.setUpdTime(rs.getString("UPD_TIME"));
				role.setActiveFlag(rs.getString("ACTIVE_FLAG"));
				list.add(role);
			}
		}catch(SQLException sqlE){
			sqlE.printStackTrace();
		}finally{
			DbPool.close(conn, pstmt, rs);
		}
		return list;
	}
	public static List<Role> getRoleListByUser(String account){
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		
		List<Role> list = new ArrayList<Role>();
		String strsql = "SELECT * FROM WL_ROLE WHERE ROLE_ID IN " +
				" (SELECT ROLE_ID FROM WL_USER_ROLE WHERE USER_ACCOUNT=?) ";
		try{
			conn = DbPool.getConnection();
			pstmt = conn.prepareStatement(strsql);
			pstmt.setString(1,account);
			rs = pstmt.executeQuery();
			while(rs.next()){
				Role role = new Role();
				role.setRoleId(rs.getString("ROLE_ID"));
				role.setRoleName(rs.getString("ROLE_NAME"));
				role.setRoleDesc(rs.getString("ROLE_DESC"));
				role.setAddBy(rs.getString("ADD_BY"));
				role.setAddDate(rs.getString("ADD_DATE"));
				role.setAddTime(rs.getString("ADD_TIME"));
				role.setUpdBy(rs.getString("UPD_BY"));
				role.setUpdDate(rs.getString("UPD_DATE"));
				role.setUpdTime(rs.getString("UPD_TIME"));
				role.setActiveFlag(rs.getString("ACTIVE_FLAG"));
				list.add(role);
			}
		}catch(SQLException sqlE){
			sqlE.printStackTrace();
		}finally{
			DbPool.close(conn, pstmt, rs);
		}
		return list;
	}
}
